12 Seamstress::Seamstress()
19 pthread_mutexattr_init(&mattr);
20 pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_NORMAL);
21 pthread_mutex_init(&
mutex, &mattr);
22 pthread_cond_init(&cond, NULL);
23 pthread_cond_init(&waitcond, NULL);
24 pthread_attr_init(&attr);
25 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
36 pthread_mutexattr_init(&mattr);
37 pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_NORMAL);
38 pthread_mutex_init(&
mutex, &mattr);
39 pthread_cond_init(&cond, NULL);
40 pthread_cond_init(&waitcond, NULL);
41 pthread_attr_init(&attr);
42 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
46 Seamstress::~Seamstress()
48 pthread_mutex_lock(&
mutex);
51 pthread_cond_wait(&waitcond, &
mutex);
53 pthread_mutex_unlock(&
mutex);
55 pthread_attr_destroy(&attr);
56 pthread_mutex_destroy(&
mutex);
57 pthread_mutexattr_destroy(&mattr);
58 pthread_cond_destroy(&cond);
59 pthread_cond_destroy(&waitcond);
63 void *Seamstress::prepare(
void *arg)
66 while((seamstress->
end)==
false)
68 pthread_mutex_lock(&(seamstress->
mutex));
69 while(seamstress->
gotime==
false)
71 pthread_cond_wait(&(seamstress->
cond), &(seamstress->
mutex));
75 seamstress->
end =
true;
76 pthread_mutex_unlock(&(seamstress->
mutex));
79 pthread_mutex_unlock(&(seamstress->
mutex));
82 seamstress->
gotime =
false;
86 pthread_mutex_lock(&(seamstress->
mutex));
89 pthread_mutex_unlock(&(seamstress->
mutex));
90 pthread_cond_signal(&(seamstress->
waitcond));
92 pthread_mutex_lock(&(seamstress->
mutex));
95 pthread_mutex_unlock(&(seamstress->
mutex));
96 pthread_cond_signal(&(seamstress->
waitcond));
103 pthread_mutex_lock(&
mutex);
111 pthread_mutex_unlock(&
mutex);
112 pthread_create(&pthread, &attr, &Seamstress::prepare,
this);
116 pthread_mutex_unlock(&
mutex);
121 void Seamstress::stop()
123 pthread_mutex_lock(&
mutex);
126 pthread_mutex_unlock(&
mutex);
127 pthread_cond_signal(&cond);
131 void Seamstress::sew()
133 pthread_mutex_lock(&
mutex);
136 pthread_mutex_unlock(&
mutex);
137 pthread_cond_signal(&cond);
141 void Seamstress::rest()
143 pthread_mutex_lock(&
mutex);
146 pthread_cond_wait(&waitcond, &
mutex);
148 pthread_mutex_unlock(&
mutex);
152 void Seamstress::init_vector(
unsigned long int N, vector<Seamstress> &vec)
157 for(
unsigned int i=0;i<
N;i++)
161 for(
unsigned int i=0;i<
N;i++)
168 vector<Seamstress*>* Seamstress::create_vector(
unsigned long int N)
170 vector<Seamstress*>* vec =
new vector<Seamstress*>();
171 for(
unsigned long int i=0;i<
N;i++){vec->push_back(
new Seamstress());}
172 for(
unsigned long int i=0;i<
N;i++){(*vec)[i]->start();}