• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Test program that uses std::string object from more than one thread and
3  * that also triggers a call to __GI_strlen() (from inside strdup()). See also
4  * https://bugs.kde.org/show_bug.cgi?id=326091.
5  */
6 
7 #include <list>
8 #include <string>
9 #include <cstring>
10 #include <pthread.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 
list2byteArray()14 char* list2byteArray()
15 {
16   size_t data_size = 24;
17   char *data = new char[data_size];
18   for (size_t i = 0; i < data_size; i++)
19     data[i] = 'a';
20   data[data_size - 1] = 0;
21   char *ret = strdup(data);
22   delete[] data;
23   return ret;
24 }
25 
addRecord()26 int addRecord()
27 {
28   char *data = list2byteArray();
29   usleep(100);
30   free(data);
31   return 0;
32 }
33 
fillTable(void * ptr)34 void *fillTable(void *ptr)
35 {
36   for (int i = 0; i < 100; i++) {
37     std::string id("000");
38     id.append(1, 'a' + i);
39     std::list<std::string> record;
40     record.push_back("some data");
41     addRecord();
42   }
43   usleep(1000 * 1000);
44   return NULL;
45 }
46 
main(int argc,char * argv[])47 int main(int argc, char* argv[])
48 {
49   pthread_t thread[2];
50 
51   for (int i = 0; i < sizeof(thread)/sizeof(thread[0]); i++) {
52     int ret = pthread_create(&thread[i], NULL, &fillTable, NULL);
53     if (ret) {
54       fprintf(stderr, "Failed to create thread %d: %d\n", i, ret);
55       return 1;
56     }
57   }
58 
59   for (int i = 0; i < sizeof(thread)/sizeof(thread[0]); i++) {
60     int ret = pthread_join(thread[i], NULL);
61     if (ret != 0) {
62       fprintf(stderr, "Failed to join thread %d: %d\n", i, ret);
63       return 1;
64     }
65   }
66 
67   return 0;
68 }
69