AndreiL
Welcome to AndreiL.
Enjoy your time.
Please log in.

Windows threads

Go down

Windows threads

Post by Lucaci Andrei on Fri Nov 23, 2012 6:52 am

Not quite done, but here it is.


  1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <winsock.h>
#include <windows.h>
#include <time.h>
#define ON 1
#define OFF 1
#define ENDLL 0
#pragma comment(lib, "wsock32.lib")
typedef struct node{
double value;
int worker;
struct node* next;
}node;
int max, counter, status, pauseAll, dec;
TCHAR server_sem[]=TEXT("Global\\server_sem");;
HANDLE serv_sem, sm;
DWORD* threadId, waiterId;
CRITICAL_SECTION mtx;
CRITICAL_SECTION avmtx;
CONDITION_VARIABLE avcond;
SRWLOCK rw;
node* head;
node* create(int max);
DWORD WINAPI sqrtList (LPVOID p);
DWORD WINAPI wite(LPVOID p);
void print();
void sleepy();
void removes();
int usleep(long usec);
WORD wVersionRequested = MAKEWORD(1,0);
WSADATA wsaData;

int main(int argc, char* argv[]){
int i, * a;
HANDLE waiter;
HANDLE* threadArray;//=(HANDLE)malloc(sizeof((HANDLE*[atoi(argv[1]))+2]));
counter=0;
srand((unsigned int)time(NULL));
threadId=(DWORD*) malloc(sizeof(DWORD)*max);
serv_sem = CreateSemaphore(NULL, 3, 3, server_sem);
InitializeCriticalSection(&mtx);
WSAStartup(wVersionRequested, &wsaData);
InitializeConditionVariable(&avcond);
InitializeSRWLock(&rw);
if (argc>1)
dec=max=atoi(argv[1]);
else max=dec=14;
threadArray=(HANDLE)malloc(sizeof(HANDLE)*max);
head=create(max);
status=ON;
counter=0;
print();
waiter=CreateThread(NULL, // default security attributes
0, // use default stack size
wite, // thread function name
NULL, // argument to thread function
0, // use default creation flags
&waiterId);
Sleep(1);
for (i=0;i<max;i++){
WaitForSingleObject(serv_sem, INFINITE);
a=(int*)malloc(sizeof(int));
a[0]=i;
usleep(100);
//printf("%d\n",a[0]);
threadArray[i] = CreateThread(
NULL, // default security attributes
0, // use default stack size
sqrtList, // thread function name
a, // argument to thread function
0, // use default creation flags
&threadId[i]);
ReleaseSemaphore(serv_sem, 1, NULL);
}

//while(status==ON)
usleep(1);

WaitForMultipleObjects(max, threadArray, TRUE, INFINITE);
WaitForSingleObject(waiter, INFINITE);
//for(i=0; i<max; i++){
// CloseHandle(threadArray[i]);
// }
CloseHandle(waiter);

if (head==NULL) printf("List is empty.\n");
else {
printf("workre: %d\n", head->worker);
printf("Remaining itmes in the list: \n");
print();
}

CloseHandle(serv_sem);
DeleteCriticalSection(&mtx);
DeleteCriticalSection(&avmtx);
//free(threadArray);
free(a);
printf("OK!");

return 0;
}

void print(){
node* it=head;
while(it!=NULL){
printf("Value: %lf\t\t|\t\tWorker: %d\n", it->value, it->worker);
it=it->next;
}
}

void sleepy(){
while(pauseAll!=1) usleep(100);
pauseAll=0;
}

struct node* create(int max){
node* head=(struct node*) malloc(sizeof(struct node)),* temp;
int i;
head->value=rand();
head->worker=0;
head->next=ENDLL;
temp=head;
printf("Max:%d\n", max);
for (i=1;i<max;i++){
struct node* elem=(struct node*) malloc(sizeof(struct node));
elem->value=rand();
elem->worker=i;
elem->next=NULL;
temp->next=elem;
temp=temp->next;
}
return head;
}

void removes(){
node* it,* temp;
while(head!=NULL && head->value<2){
node* it=head->next;
printf("Value: %lf\t\t|\t\tWorker: %d\n", head->value, head->worker);
head=it;
}
it=head;
if (head==ENDLL) return;
while(it->next!=ENDLL){//it->next!=ENDLL){
if (it->next->value<2){
printf("Value: %lf\t\t|\t\tWorker: %d\n", it->next->value, it->next->worker);
temp=it->next->next;
free(it->next);
it->next=temp;
}
else {
it=it->next;
}
}
}

DWORD WINAPI sqrtList (void* p){
int i, cont=0;
node* pointer;
i=*(int*)p;
pointer=head;
while(status==ON && pointer!=NULL){
AcquireSRWLockExclusive(&rw);
//printf("%d\n",i);
cont++;
ReleaseSRWLockExclusive(&rw);
if (pointer->worker==i){
AcquireSRWLockExclusive(&rw);
counter++;
ReleaseSRWLockExclusive(&rw);
while(pointer->value>=2)
pointer->value=sqrt(pointer->value);
if (counter==max){
printf("Signaled - end of the list.\n");
AcquireSRWLockExclusive(&rw);
status=OFF;
ReleaseSRWLockExclusive(&rw);
WakeConditionVariable(&avcond);
}
else if (counter!=0 && counter%5==0){
printf("Signaled - 5 elements.\n");
WakeConditionVariable(&avcond);
sleepy();
}
usleep(100);
break;
}
else pointer=pointer->next;
}
return 0;
}

DWORD WINAPI wite(LPVOID p){
EnterCriticalSection(&mtx);
while(status==ON){
printf("Waiting for signal: \n");
SleepConditionVariableCS(&avcond, &mtx, INFINITE);
printf("Signal received.\nDeleted items: \n");
removes();
printf("Items remaining: %d.\n", max-counter);
//pauseAll=1;
if (head==NULL) status=OFF;
if(counter==max) break;
}
LeaveCriticalSection(&mtx);
return 0;
}

int usleep(long usec){
struct timeval tv;
fd_set dummy;
SOCKET s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
FD_ZERO(&dummy);
FD_SET(s, &dummy);
tv.tv_sec = usec/1000000L;
tv.tv_usec = usec%1000000L;
return select(0, 0, 0, &dummy, &tv);
}

avatar
Lucaci Andrei
"Tata Lor"


Number of messages : 222
Points : 2266743
Reputation : 1007
Registration date : 2008-08-15
Age : 26
Location : Cluj-Napoca

View user profile http://www.andreil.wgz.ro

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum