You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
190 lines
3.7 KiB
190 lines
3.7 KiB
#if 0
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <pthread.h>
|
|
#include <unistd.h>
|
|
|
|
void *new_thread (void *arg)
|
|
{
|
|
printf("A new Thread Created!");
|
|
return arg;
|
|
}
|
|
|
|
int main ()
|
|
{
|
|
pthread_t tid;
|
|
int checking;
|
|
|
|
checking = pthread_create(&tid, NULL, new_thread, NULL);
|
|
|
|
if (checking !=0)
|
|
perror("create Thread");
|
|
|
|
pthread_exit(NULL);
|
|
}
|
|
#endif
|
|
|
|
#if 1
|
|
|
|
#include <stdint.h>
|
|
#include <unistd.h>
|
|
#include <iostream>
|
|
#include <functional>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <pthread.h>
|
|
#include <cstdlib>
|
|
|
|
|
|
static void* task_1(void* arg)
|
|
{
|
|
int i = 0;
|
|
|
|
//for(i = 0; i < 10; i++) {
|
|
while(1) {
|
|
std::cout << "Hello, world!" << std::endl;
|
|
usleep(1000000);
|
|
}
|
|
|
|
|
|
while(1) usleep(10000);
|
|
};
|
|
|
|
static void* task_2(void* arg)
|
|
{
|
|
while(1)
|
|
{
|
|
std::cout << "Task 2"<< std::endl;
|
|
usleep(10000000);
|
|
}
|
|
};
|
|
|
|
|
|
int main(void)
|
|
{
|
|
pthread_t th_task_1;
|
|
pthread_t th_task_2;
|
|
|
|
int check = 0;
|
|
|
|
std::cout << "threads test" << std::endl;
|
|
|
|
check = pthread_create(&th_task_1,
|
|
NULL,
|
|
&task_1,
|
|
NULL);
|
|
if(check != 0) {
|
|
std::cout << "Failed to create thread!" << std::endl;
|
|
exit(-1);
|
|
}
|
|
|
|
|
|
check = pthread_create(&th_task_2,
|
|
NULL,
|
|
&task_2,
|
|
NULL);
|
|
if(check != 0) {
|
|
std::cout << "Failed to create thread!" << std::endl;
|
|
exit(-1);
|
|
}
|
|
|
|
pthread_join(th_task_1,NULL);
|
|
pthread_join(th_task_2,NULL);
|
|
|
|
|
|
return 0;
|
|
}
|
|
#endif
|
|
|
|
#if 0
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <pthread.h>
|
|
/* 100000 Elemente */
|
|
#define MAX 100000
|
|
|
|
/* ein Array von großen zu kleinen Werten */
|
|
int test_array1[MAX];
|
|
int test_array2[MAX];
|
|
|
|
/* in umgekehrter Reihenfolge erstellen */
|
|
void init_test_array(int *array) {
|
|
int i, j;
|
|
for(i = MAX,j=0; i >= 0; i--,j++)
|
|
array[j] = i;
|
|
}
|
|
|
|
// Thread 1
|
|
static void *bubble1(void* val) {
|
|
static int i, temp, elemente=MAX;
|
|
printf("Thread bubble1() wurde gestartet\n");
|
|
while(elemente--)
|
|
for(i = 1; i <= elemente; i++)
|
|
if(test_array1[i-1] > test_array1[i]) {
|
|
temp=test_array1[i];
|
|
test_array1[i]=test_array1[i-1];
|
|
test_array1[i-1]=temp;
|
|
}
|
|
printf("Thread bubble1() wurde beendet\n");
|
|
// Der Rückgabewert interessiert uns nicht.
|
|
return NULL;
|
|
}
|
|
|
|
// Thread 2
|
|
static void *bubble2(void* val) {
|
|
static int i, temp, elemente=MAX;
|
|
printf("Thread bubble2() wurde gestartet\n");
|
|
while(elemente--)
|
|
for(i = 1; i <= elemente; i++)
|
|
if(test_array2[i-1] > test_array2[i]) {
|
|
temp=test_array2[i];
|
|
test_array2[i]=test_array2[i-1];
|
|
test_array2[i-1]=temp;
|
|
}
|
|
printf("Thread bubble2() wurde beendet\n");
|
|
// Der Rückgabewert interessiert uns nicht.
|
|
return NULL;
|
|
}
|
|
|
|
|
|
int main (void) {
|
|
pthread_t thread1, thread2;
|
|
int i, rc;
|
|
|
|
// Ausgabe in eine Textdatei
|
|
freopen("myoutput.txt", "w+", stdout);
|
|
|
|
printf("Haupt-Thread main() wurde gestartet\n");
|
|
// beide Arrays mit Werten initialisieren
|
|
init_test_array(test_array1);
|
|
init_test_array(test_array2);
|
|
// Thread 1 erzeugen
|
|
rc = pthread_create( &thread1, NULL, &bubble1, NULL );
|
|
if( rc != 0 ) {
|
|
printf("Konnte Thread 1 nicht erzeugen\n");
|
|
return EXIT_FAILURE;
|
|
}
|
|
// Thread 2 erzeugen
|
|
rc = pthread_create( &thread2, NULL, &bubble2, NULL );
|
|
if( rc != 0 ) {
|
|
printf("Konnte Thread 2 nicht erzeugen\n");
|
|
return EXIT_FAILURE;
|
|
}
|
|
// Main-Thread wartet auf beide Threads.
|
|
pthread_join( thread1, NULL );
|
|
pthread_join( thread2, NULL );
|
|
|
|
// das Ergebnis der Sortierung in die Datei
|
|
// myoutput.txt schreiben
|
|
for(i = 0; i < MAX; i++) {
|
|
printf("[%d-%d]", test_array1[i], test_array2[i]);
|
|
}
|
|
printf("\nHaupt-Thread main() wurde beendet\n");
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
#endif
|
|
|
|
|