Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
c-stuff
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bruce Cowan
c-stuff
Commits
157fc91b
Commit
157fc91b
authored
6 years ago
by
Bruce Cowan
Browse files
Options
Downloads
Patches
Plain Diff
Add pthread example
parent
9f99a2d7
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
meson.build
+1
-0
1 addition, 0 deletions
meson.build
pthread/meson.build
+4
-0
4 additions, 0 deletions
pthread/meson.build
pthread/thread-incr.c
+75
-0
75 additions, 0 deletions
pthread/thread-incr.c
with
80 additions
and
0 deletions
meson.build
+
1
−
0
View file @
157fc91b
...
...
@@ -36,4 +36,5 @@ subdir('array')
subdir
(
'hashtable'
)
subdir
(
'list'
)
subdir
(
'openmp'
)
subdir
(
'pthread'
)
subdir
(
'winter'
)
This diff is collapsed.
Click to expand it.
pthread/meson.build
0 → 100644
+
4
−
0
View file @
157fc91b
executable
(
'thread-incr'
,
'thread-incr.c'
,
dependencies
:
thread_dep
,
link_with
:
lib_utils
,
include_directories
:
incdir
)
This diff is collapsed.
Click to expand it.
pthread/thread-incr.c
0 → 100644
+
75
−
0
View file @
157fc91b
#include
<stdio.h>
#include
<stdlib.h>
#include
<pthread.h>
#include
<utils.h>
typedef
struct
{
pthread_t
thread
;
int
id
;
int
loops
;
}
ThreadData
;
static
volatile
int
glob
=
0
;
static
pthread_mutex_t
mtx
=
PTHREAD_MUTEX_INITIALIZER
;
static
void
err_exit
(
const
char
*
func
)
{
fprintf
(
stderr
,
"Error: %s
\n
"
,
func
);
}
static
void
*
thread_func
(
void
*
arg
)
{
ThreadData
*
data
=
(
ThreadData
*
)
arg
;
for
(
int
j
=
0
;
j
<
data
->
loops
;
j
++
)
{
if
(
pthread_mutex_lock
(
&
mtx
))
err_exit
(
"pthread_mutex_lock"
);
printf
(
"Thread #%d: glob = %d
\n
"
,
data
->
id
,
glob
);
int
loc
=
glob
;
loc
++
;
glob
=
loc
;
if
(
pthread_mutex_unlock
(
&
mtx
))
err_exit
(
"pthread_mutex_unlock"
);
}
return
NULL
;
}
int
main
(
int
argc
,
char
**
argv
)
{
int
*
loops
=
get_ints
(
argc
,
argv
,
1
,
"Number of loops"
);
ThreadData
*
t1
=
malloc
(
sizeof
(
ThreadData
));
t1
->
id
=
1
;
t1
->
loops
=
*
loops
;
if
(
pthread_create
(
&
t1
->
thread
,
NULL
,
thread_func
,
t1
))
err_exit
(
"pthread_create"
);
ThreadData
*
t2
=
malloc
(
sizeof
(
ThreadData
));
t2
->
id
=
2
;
t2
->
loops
=
*
loops
;
if
(
pthread_create
(
&
t2
->
thread
,
NULL
,
thread_func
,
t2
))
err_exit
(
"pthread_create"
);
if
(
pthread_join
(
t1
->
thread
,
NULL
))
err_exit
(
"pthread_join"
);
if
(
pthread_join
(
t2
->
thread
,
NULL
))
err_exit
(
"pthread_join"
);
printf
(
"End: glob = %d
\n
"
,
glob
);
free
(
t1
);
free
(
t2
);
return
0
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment