Questions about conditional locks and mutex locks (pthread_cond_t and pthread_mutex_t)

Source: Internet
Author: User

The principle of mutex lock has been considered in the past two days.CodeTesting has become increasingly confusing.

 

# Include <stdio. h> <br/> # include <stdlib. h> <br/> # include <string. h> <br/> # include <pthread. h> </P> <p> pthread_cond_t _ cond; <br/> pthread_mutex_t condmutex; </P> <p> class test <br/>{< br/> public: <br/> static void func () <br/> {<br/> // pthread_mutex_t hsingletonmutex; <br/> // pthread_mutex_init (& hsingletonmutex, null ); </P> <p> if (I = 0) <br/>{< br/> // pthread_mutex_lock (& hsingletonmutex); <br/> I ++; <br/>}< Br/> // pthread_mutex_unlock (& hsingletonmutex); <br/> // pthread_mutex_destroy (& hsingletonmutex); <br/>}</P> <p> static int Geti () <br/>{< br/> return I; <br/>}</P> <p> protected: <br/> test () {}</P> <p> virtual ~ Test () {}</P> <p> PRIVATE: <br/> static int I; </P> <p> }; </P> <p> int test: I = 0; </P> <p> void threadfunc () <br/>{< br/> pthread_mutex_lock (& condmutex); <br/> pthread_cond_wait (& _ cond, & condmutex ); </P> <p> // printf ("Wait singal finished/N"); <br/> test: func (); </P> <p> pthread_mutex_unlock (& condmutex); <br/>}</P> <p> void threadfunc2 () <br/>{< br/> pthread_mutex_lock (& condmutex); <br/> pthread_cond_broadcast (& _ Cond); <br/> // pthread_cond_signal (& _ Cond ); <br/> pthread_mutex_unlock (& condmutex); <br/>}</P> <p> int main (INT argc, char ** argv) <br/>{< br/> pthread_mutex_init (& condmutex, null); <br/> pthread_cond_init (& _ cond, null ); </P> <p> unsigned int COUNT = 10; <br/> If (argc = 2) <br/>{< br/> COUNT = atoi (argv [1]); <br/>}</P> <p> pthread_t t_id [count]; <br/> pthread_t thread; <br/> int I = 0; </P> <p> for (; I <count; I ++) <br/> {<br/> pthread_create (& t_id [I], null, (void * (*) (void *) threadfunc, null ); <br/>}</P> <p> printf ("create % d thread finished/N", count); <br/> pthread_create (& Thread, null, (void * (*) (void *) threadfunc2, null); <br/> printf ("create another thread finished/N "); </P> <p> for (I = 0; I <count; I ++) <br/>{< br/> pthread_join (t_id [I], null ); <br/>}</P> <p> pthread_cond_destroy (& _ Cond); <br/> pthread_mutex_destroy (& condmutex ); </P> <p> printf ("I = % d/N", test: Geti (); </P> <p> return 0; <br/>}< br/>

 

The test class simulates the singleton class. In order to make multiple threads concurrent, the conditional lock is added. When func () is not locked, I is always equal to 1, it indicates that there are no non-synchronous threads, but there is a problem:

1. When the conditional lock is added to main, that is, singal is triggered in the process. Wait in the thread is still blocked, but there is no problem when another independent thread triggers Singal. Are these locks only prepared for the thread?

2. when pthread_cond_signal (& _ Cond) is used, only one thread can be triggered without waiting. Which thread will be scheduled by the system to start working, and other threads are still blocked by wait, therefore, all threads can be started when broadcast is used.

3. In the static void func () function, is it useful to initialize a local variable lock? Does it have to be global or member variables, and local variables cannot be used? I cannot try this.

4. After simulating for half a day, after receiving the singal thread and starting to work at the same time, there was no I exception. Why?

 

Awaiting Solution ......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.