C++ introduced the mutex header, which USES mutext.lock() to lock and mutex.unlock() to release the lock.
#include <mutex>
using namespace std;
mutex t_mutex;
class Csingleob
{
private:
Csingleob(){}
static Csingleob *p;
public:
static Csingleob* getInstance()
{
mutex.lock();
if (p == NULL)
{
p = new Csingleob();
}
mutex.unlock();
return p;
}
};
However, an error is reported at compile time. Prompt:
error: expected unqualified-id before ‘.’ token mutex.lock();
It is an error to use mutext.lock() to lock, and mutex.unlock() to release the lock. You should instantiate the class or structure first, then call the corresponding method with “.”
After modification:
#include <mutex>
using namespace std;
mutex t_mutex;
class Csingleob
{
private:
Csingleob(){}
static Csingleob *p;
public:
static Csingleob* getInstance()
{
t_mutex.lock();
if (p == NULL)
{
p = new Csingleob();
}
t_mutex.unlock();
return p;
}
};
Csingleob* Csingleob::p = NULL;
div>
Read More:
- Expected unqualified ID before string constant
- Expected unqualified ID before numeric constant
- error: expected unqualified-id before ‘int’
- Syntax error on token “eat”, identifier expected after this token
- error: expected unqualified-id before ‘dynamic_cast’
- Error: expected class name before ‘{‘ token
- syntax error on token “catch”,Identifier expected
- Token bucket implementation with adaptive token generation rate
- About writing [if] and [else] statements in JSP, Syntax error on token “else”, delete this token
- Syntax error on token “}“, delete this token
- Uncaught syntax error: unexpected token ‘< 0‘
- JWT and token + redis scheme of spring security
- Solve the Java “syntax error on token enum” problem
- Parsing error: Unexpected token
- 107_ Error report and solution of erc20 token transfer
- [Solved] Access /oauth/token in SpringCloud OAuth2 and report server_error
- syntax error near unexpected token `then’ problem solution
- After SAP Spartacus successfully logs in, does the request base site need access token
- Error resolution of unexpected token in JSON at position 0